Reading XML with the XmlReader class in C#

C #: There are two main ways to read XML with XmlDocument class and XmlReader class. XmlDocument reads the entire XML content in memory and then lets you move forward, as you please, or even ask documents by using XPath technology.

XmlReader is a fast and low memory-consuming option, it allows you to run XML content at a time in a while, while allowing you to see the value, and after that, by going ahead on the next element, by doing this, it is clear Uses very little memory as it is only the current element and because you have to manually examine the values, you will only get the relevant element, which it flows Faster will make it faster. In this chapter, we will focus on the XmlReader class, and then go to the XmlDocument class in the next chapter.

Let's try a small example, where we read an XML document with currency rate, lucky for us, one of the European Central Bank that we can use, we can download it and read it with harddrive Yes, but in reality, both the XmlReader and XmlDocument classes can read XML with a remote URL as well as a local file you can see XML here (http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml) and here some code comes and an explanation about it:


using System;
using System.Text;
using System.Xml;

namespace ParsingXml
{
    class Program
    {
        static void Main(string[] args)
        {            
            XmlReader xmlReader = XmlReader.Create("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
            while(xmlReader.Read())
            {
                if((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "Cube"))
                {
                    if(xmlReader.HasAttributes)
                        Console.WriteLine(xmlReader.GetAttribute("currency") + ": " + xmlReader.GetAttribute("rate"));                    
                }
            }
            Console.ReadKey();
        }
    }
}

We start using the Static Build () method by making an XmlReader example. It has many overloads, but the easiest of them point to the URL in the file. In some time loop, we read the read () method on the XmlReader example. From this, the reader progresses in the next element and by then it is more readable, it gives the correct return, inside the loop, we can now use one of the many properties and methods on XmlReader to access the data from the current element. .

In our case, we check the node type that we have an element (tag part), and if its name is "cube", as you can see in the XML document we used, every currency rate is cube It is within an element, so we want it - balance is ignored after we have a cube element, we do a formal investigation to see if there are attributes (which it should) and then we Attribute value of "currency" and "rate using GetAttribute () method to read using". We print them in the console and then we go to the next element. The result should be a complete list of currencies and their current exchange rate.

As you can see, it is very easy to do, but mainly because we need data in the same order, as they are taught, without it, without fancy things. In the next chapter, we will do the same thing but use the XMLDC class so that you can see the difference.

Note: You should know that there are several ways in which the above code can fail and throw an exception, which you will definitely handle. See the chapter on exception for more information